
$(document).ready(init);

function init(){
	
	initData();
	renderPage();
	bindEvent();
}

function initData(){
	queryMode = _QUERY_MODE_SIMPLE;
}

function bindEvent(){
	$(document).bind(_EVENT_PROMPT_INFO,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_WARNING,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_ERROR,ComponentUtil.promptMsg);
	
	$('#showAdvancedQueryFormLink').click(ComponentUtil.showAdvancedQueryForm);
	$('#hideAdvancedQueryFormLink').click(ComponentUtil.hideAdvancedQueryForm);
	
	$('#simpleQueryBt').click(sendQueryResult);
	$('#advanceQueryBt').click(sendQueryResult);
	
}

function renderPage(){
	
	var addBt = {text:'新增角色',iconCls:'icon-add',handler:popupAddWindow};
	var removeBt = {text:'删除角色',iconCls:'icon-cancel',handler:confirmRemove };
	//var acceptBt = {text:'保存变更',iconCls:'icon-ok',handler:acceptChanges };
	//var rejectBt = {text:'取消变更',iconCls:'icon-undo',handler:rejectChanges };
	
	
	$("#roleTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:true,
		idField:'id',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/roleManager/listRole',
		toolbar:[addBt,'-',removeBt,'-'],
		loadMsg:'加载数据中,请稍等...',
		onDblClickRow:popupModifyWindow
	});
	
	ComponentUtil.localizePagination($('#roleTable').datagrid('getPager'));
	
	
	
	$("#privilegeTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:false,
		idField:'code',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/privilegeManager/listPrivilege',
		loadMsg:'加载数据中,请稍等...'
	});
	
	$("#roleDetailDiv").dialog({
		width:550,
		height:380,
		resizable:false,
		closed:true
	});
	$.messager.defaults = {ok:'确认',cancel:'取消'};
	
}

function popupModifyWindow(rowIndex, rowData){
	
	var saveButton = {
		text:'保存变更',
		iconCls:'icon-save',
		handler:function(){commitRoleUpdate(rowData.id);}
	}
	var cancelButton = {
		text:'取消',
		handler:closeDetailWindow
	};
	
	$("#roleDetailDiv").dialog({
		closed:false,
		modal:true,
		buttons:[saveButton,cancelButton]
	});
	
	$('#roleCode').val(rowData.code);
	$('#description').val(rowData.description);
	
	var privileges = rowData.privileges.split(",");
	$('#privilegeTable').datagrid('clearSelections');
	$('#privilegeTable').datagrid('reload');
	for(var i=0;i<privileges.length;i++){
		$("#privilegeTable").datagrid('selectRecord',privileges[i]);
	}
	
	
}


function popupAddWindow(){
	var addButton = {
		text:'新增角色',
		iconCls:'icon-add',
		handler:addRole
	};
	
	var cancelButton = {
		text:'取消',
		handler:closeDetailWindow
	};
	
	
	$("#roleDetailDiv").dialog({
		closed:false,
		modal:true,
		buttons:[addButton,cancelButton]
	});
	
	
	$('#roleCode').val('');
	$('#description').val('');
	$('#privilegeTable').datagrid('clearSelections');
	$('#privilegeTable').datagrid('reload');
}

function closeDetailWindow(){
	$("#roleDetailDiv").dialog('close');
}

function addRole(){
	if (!$('#privilegeForm').form('validate')) return;
	
	var privileges='';
	
	var rows = $('#privilegeTable').datagrid('getSelections');
	for(var i=0;i<rows.length;i++){
		privileges += rows[i].code+',';
	}
	if (privileges.length>0){
		privileges = privileges.substring(0,privileges.length-1);
	}
	
	var code = $.trim($('#roleCode').val());
	var description = $.trim($('#description').val());
	
	var role = {code:code,privileges:privileges,description:description};
	$.post(webappName+"/admin/roleManager/addRole",$.param(role),onAddRoleComplete,"json");
	closeDetailWindow();
}

function onAddRoleComplete(data){
	$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	$("#roleTable").datagrid('reload');
}

function confirmRemove(){
	var rows = $('#roleTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要删除的项目');
		return;
	}
	$.messager.confirm('确认','确认要删除吗?',removeRole);
}

function removeRole(isConfirm){
	if (!isConfirm) return;
	
	var rows = $('#roleTable').datagrid('getSelections');
	var role;
	var codeList='';
	for(var i=0;i<rows.length;i++){
		role = rows[i];
		codeList += role.code + ',';
	}
	codeList = codeList.substring(0,codeList.length-1);
	
	var requestPara = 'roleList='+codeList;
	
	$.post(webappName+"/admin/roleManager/removeRoles",requestPara,onRemoveRoleComplete,"json");
	
}

function onRemoveRoleComplete(data){
	
	$("#roleTable").datagrid('reload');
	
	var result = data.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
	

}



function commitRoleUpdate(roleId){
	
	var privilegesSelected = $('#privilegeTable').datagrid('getSelections');
	var privileges='';
	for(var i=0;i<privilegesSelected.length;i++){
		privileges += privilegesSelected[i].code+',';
	}
	if (privileges.length>0){
		privileges = privileges.substring(0,privileges.length-1);
	}
	var code = $.trim($('#roleCode').val());
	var description = $.trim($('#description').val());
	var roleUpdated = {id:roleId,code:code,description:description,privileges:privileges};
	
	$.post(webappName+"/admin/roleManager/updateRole",$.param(roleUpdated),onUpdateRoleComplete,"json");
}

function onUpdateRoleComplete(responseVO){
	
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"更新成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	$("#roleTable").datagrid('reload');
	closeDetailWindow();
}



function sendQueryResult(){
	
	var queryParams = $('#roleTable').datagrid('options').queryParams;
	queryParams.queryMode = queryMode;
	queryParams.simpleQueryString = $.trim($('#simpleQueryConditionTxt').val());
	queryParams.code = $.trim($('#roleCodeTxt').val());
	queryParams.privileges = $.trim($('#privilegeTxt').val());
	queryParams.description = $.trim($('#roleDescriptionTxt').val());
	$("#roleTable").datagrid('load');  
}